﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ActionFilterPlugin.models
{
    public static class SecurityHelper
    {
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string DESEncrypt(string source, string sKey)
        {
            int requiredKeyLength = 8;
            if (string.IsNullOrEmpty(sKey))
                return string.Empty;
            if (sKey.Length > requiredKeyLength)
                sKey = sKey.Substring(0, requiredKeyLength);
            using (DES des = DES.Create())
            {
                byte[] inputByteArray = Encoding.Default.GetBytes(source);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        StringBuilder ret = new StringBuilder();
                        foreach (byte b in ms.ToArray())
                        {
                            ret.AppendFormat("{0:X2}", b);
                        }
                        ret.ToString();
                        return ret.ToString();
                    }
                }
            }
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string DESDecrypt(string source, string sKey)
        {
            int requiredKeyLength = 8;
            if (string.IsNullOrEmpty(sKey))
                return string.Empty;
            if (sKey.Length > requiredKeyLength)
                sKey = sKey.Substring(0, requiredKeyLength);
            using (DES des = DES.Create())
            {

                byte[] inputByteArray = new byte[source.Length / 2];
                for (int x = 0; x < source.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(source.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (var t = des.CreateDecryptor())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, t, CryptoStreamMode.Write))
                        {
                            cs.Write(inputByteArray, 0, inputByteArray.Length);
                            cs.FlushFinalBlock();
                            StringBuilder ret = new StringBuilder();
                            return System.Text.Encoding.Default.GetString(ms.ToArray());
                        }
                    }
                }
            }
        }
    }
}
